const fs = require('fs');
const path = require('path');
const readline = require('readline');

const directoryPath = path.join(__dirname, 'src');

// 定义一个函数来移除文件中的 console.log 语句
function removeConsoleLogs(filePath) {
  const tempFilePath = filePath + '.tmp';
  const readStream = fs.createReadStream(filePath);
  const writeStream = fs.createWriteStream(tempFilePath);
  const rl = readline.createInterface({
    input: readStream,
    output: writeStream,
    terminal: false,
  });

  rl.on('line', (line) => {
    if (!line.trim().startsWith('console.log')) {
      writeStream.write(line + '\n');
    }
  });

  rl.on('close', () => {
    fs.renameSync(tempFilePath, filePath);
  });
}

// 定义一个函数来遍历目录中的所有文件
function traverseDirectory(directory) {
  fs.readdir(directory, (err, files) => {
    if (err) throw err;

    files.forEach(file => {
      const filePath = path.join(directory, file);
      if (fs.lstatSync(filePath).isDirectory()) {
        traverseDirectory(filePath);
      } else if (path.extname(file) === '.jsx') {
        removeConsoleLogs(filePath);
      }
    });
  });
}

// 开始遍历目录
traverseDirectory(directoryPath);
